using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DIS_DAL_WS.Models;
namespace DIS_DAL_WS
{
	#region Using
	using DIS_DAL_WS.Models;
	using DIS_DAL_WS.Repositories;
	using DIS_DAL_WS.Helpers;
	using DIS_DAL_WS.DTO;
    using CF_DAL_WS.Repositories;
    using System.Web.Hosting;
    using System.IO;
	#endregion
	public class DIS_DAL_Service: IDIS_DAL_Service
    {
        #region CATEGORY

        public List<CategoryDTO> Category_SelectAll()
		{
			var lstDto = new List<CategoryDTO>();
			var rpt = new CategoryRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(CategoryDTO.Translate(item));
			}
			return lstDto;
		}
		public CategoryDTO Category_SelectByID(int id)
		{
			var rpt = new CategoryRepository();
			return CategoryDTO.Translate(rpt.SelectByID(id));
		}
		public bool Category_Insert(CategoryDTO obj)
		{
			var rpt = new CategoryRepository();
			var entity = CategoryDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Category_Delete(int id)
		{
			var rpt = new CategoryRepository();
			return rpt.Delete(id);
		}
		public bool Category_Update(CategoryDTO obj)
		{
			var rpt = new CategoryRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<CategoryDTO> Category_RetrieveByID(int id)
		{
			var rpt = new CategoryRepository();
			var lstDto = new List<CategoryDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(CategoryDTO.Translate(item));
			}
			return lstDto;
		}

        #endregion

        #region DAILYREPORT
        
		public List<DailyReportDTO> DailyReport_SelectAll()
		{
			var lstDto = new List<DailyReportDTO>();
			var rpt = new DailyReportRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(DailyReportDTO.Translate(item));
			}
			return lstDto;
		}
		public DailyReportDTO DailyReport_SelectByID(int id)
		{
			var rpt = new DailyReportRepository();
			return DailyReportDTO.Translate(rpt.SelectByID(id));
		}
		public bool DailyReport_Insert(DailyReportDTO obj)
		{
			var rpt = new DailyReportRepository();
			var entity = DailyReportDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool DailyReport_Delete(int id)
		{
			var rpt = new DailyReportRepository();
			return rpt.Delete(id);
		}
		public bool DailyReport_Update(DailyReportDTO obj)
		{
			var rpt = new DailyReportRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<DailyReportDTO> DailyReport_RetrieveByID(int id)
		{
			var rpt = new DailyReportRepository();
			var lstDto = new List<DailyReportDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(DailyReportDTO.Translate(item));
			}
			return lstDto;
		}
		public List<DailyReportDTO> DailyReport_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			var rpt = new DailyReportRepository();
			var lstDto = new List<DailyReportDTO>();
			var lstEntity = rpt.SelectByIDChemistWareHouse(idChemistWareHouse);
			foreach (var item in lstEntity)
			{
				lstDto.Add(DailyReportDTO.Translate(item));
			}
			return lstDto;
		}
        public DailyReportDTO DailyReport_SelectByIDChemistWareHouseAndReportDate(int idChemistWareHouse, DateTime reportdate)
        {
            var rpt = new DailyReportRepository();
            return DailyReportDTO.Translate(rpt.SelectByIDChemistWareHouseAndReportDate(idChemistWareHouse, reportdate));
        }

        #endregion

        #region WEEKLYREPORT
		
        public List<WeeklyReportDTO> WeeklyReport_SelectAll()
		{
			var lstDto = new List<WeeklyReportDTO>();
			var rpt = new WeeklyReportRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(WeeklyReportDTO.Translate(item));
			}
			return lstDto;
		}
		public WeeklyReportDTO WeeklyReport_SelectByID(int id)
		{
			var rpt = new WeeklyReportRepository();
			return WeeklyReportDTO.Translate(rpt.SelectByID(id));
		}
		public bool WeeklyReport_Insert(WeeklyReportDTO obj)
		{
			var rpt = new WeeklyReportRepository();
			var entity = WeeklyReportDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool WeeklyReport_Delete(int id)
		{
			var rpt = new WeeklyReportRepository();
			return rpt.Delete(id);
		}
		public bool WeeklyReport_Update(WeeklyReportDTO obj)
		{
			var rpt = new WeeklyReportRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<WeeklyReportDTO> WeeklyReport_RetrieveByID(int id)
		{
			var rpt = new WeeklyReportRepository();
			var lstDto = new List<WeeklyReportDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(WeeklyReportDTO.Translate(item));
			}
			return lstDto;
		}
		public List<WeeklyReportDTO> WeeklyReport_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			var rpt = new WeeklyReportRepository();
			var lstDto = new List<WeeklyReportDTO>();
			var lstEntity = rpt.SelectByIDChemistWareHouse(idChemistWareHouse);
			foreach (var item in lstEntity)
			{
				lstDto.Add(WeeklyReportDTO.Translate(item));
			}
			return lstDto;
		}
        public WeeklyReportDTO WeeklyReport_SelectByIDChemistWareHouseAndReportDate(int idChemistWareHouse, DateTime startdate, DateTime enddate)
        {
            var rpt = new WeeklyReportRepository();
            return WeeklyReportDTO.Translate(rpt.SelectByIDChemistWareHouseAndReportDate(idChemistWareHouse, startdate, enddate));
        }

        #endregion

        #region ITEM
        
		public List<ItemDTO> Item_SelectAll()
		{
			var lstDto = new List<ItemDTO>();
			var rpt = new ItemRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(ItemDTO.Translate(item));
			}
			return lstDto;
		}
		public ItemDTO Item_SelectByID(int id)
		{
			var rpt = new ItemRepository();
			return ItemDTO.Translate(rpt.SelectByID(id));
		}
		public bool Item_Insert(ItemDTO obj)
		{
			var rpt = new ItemRepository();
			var entity = ItemDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Item_Delete(int id)
		{
			var rpt = new ItemRepository();
			return rpt.Delete(id);
		}
		public bool Item_Update(ItemDTO obj)
		{
			var rpt = new ItemRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<ItemDTO> Item_RetrieveByID(int id)
		{
			var rpt = new ItemRepository();
			var lstDto = new List<ItemDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(ItemDTO.Translate(item));
			}
			return lstDto;
		}
		public List<ItemDTO> Item_SelectByIDCategory(int idcategory)
		{
			var rpt = new ItemRepository();
			var lstDto = new List<ItemDTO>();
			var lstEntity = rpt.SelectByIDCategory(idcategory);
			foreach (var item in lstEntity)
			{
				lstDto.Add(ItemDTO.Translate(item));
			}
			return lstDto;
		}

        public void DeleteFile(string filename)
        {
            try
            {
                FileInfo file = new FileInfo(filename);
                if (file.Exists)
                {
                    file.Delete();
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
        }

        public bool Item_UploadFile(string fileName, byte[] buffer, string oldname = "")
        {
            GlobalVariables.Logger.Info(fileName);
            string basePath = HostingEnvironment.ApplicationPhysicalPath + @"\Images\";
            string apPath = basePath + fileName;
            GlobalVariables.Logger.Info(apPath);

            if (oldname != "" && oldname != "noimages.png")
            {
                DeleteFile(basePath + oldname);
            }

            try
            {
                using (FileStream fs = new FileStream(apPath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fs.Write(buffer, 0, (int)buffer.Length);
                    fs.Close();
                    fs.Dispose();
                }
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return false;
        }

        public byte[] Item_DownloadFile(string fileName)
        {
            string apPath = HostingEnvironment.ApplicationPhysicalPath + @"\Images\" + fileName;
            byte[] img = null;

            try
            {
                FileStream fs = new FileStream(apPath, FileMode.OpenOrCreate, FileAccess.Read);
                img = new byte[fs.Length];
                fs.Read(img, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                fs.Dispose();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return img;
        }

        #endregion

        #region EMPLOYEE
        
		public List<EmployeeDTO> Employee_SelectAll()
		{
			var lstDto = new List<EmployeeDTO>();
			var rpt = new EmployeeRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(EmployeeDTO.Translate(item));
			}
			return lstDto;
		}
		public EmployeeDTO Employee_SelectByID(int id)
		{
			var rpt = new EmployeeRepository();
			return EmployeeDTO.Translate(rpt.SelectByID(id));
		}
		public bool Employee_Insert(EmployeeDTO obj)
		{
			var rpt = new EmployeeRepository();
			var entity = EmployeeDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Employee_Delete(int id)
		{
			var rpt = new EmployeeRepository();
			return rpt.Delete(id);
		}
		public bool Employee_Update(EmployeeDTO obj)
		{
			var rpt = new EmployeeRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<EmployeeDTO> Employee_RetrieveByID(int id)
		{
			var rpt = new EmployeeRepository();
			var lstDto = new List<EmployeeDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(EmployeeDTO.Translate(item));
			}
			return lstDto;
		}
		public List<EmployeeDTO> Employee_SelectByIDRole(int idrole)
		{
			var rpt = new EmployeeRepository();
			var lstDto = new List<EmployeeDTO>();
			var lstEntity = rpt.SelectByIDRole(idrole);
			foreach (var item in lstEntity)
			{
				lstDto.Add(EmployeeDTO.Translate(item));
			}
			return lstDto;
		}
		public List<EmployeeDTO> Employee_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			var rpt = new EmployeeRepository();
			var lstDto = new List<EmployeeDTO>();
			var lstEntity = rpt.SelectByIDChemistWareHouse(idChemistWareHouse);
			foreach (var item in lstEntity)
			{
				lstDto.Add(EmployeeDTO.Translate(item));
			}
			return lstDto;
		}
        public EmployeeDTO Employee_SelectByUserName(string username)
        {
            var rpt = new EmployeeRepository();
            return EmployeeDTO.Translate(rpt.SelectByUserName(username));
        }
        public EmployeeDTO Employee_SelectByUserNameAndPassword(string username, string password)
        {
            var rpt = new EmployeeRepository();
            return EmployeeDTO.Translate(rpt.SelectByUserNameAndPassword(username, password));
        }

        #endregion

        #region NATIONALPROMOTION
        
		public List<NationalPromotionDTO> NationalPromotion_SelectAll()
		{
			var lstDto = new List<NationalPromotionDTO>();
			var rpt = new NationalPromotionRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(NationalPromotionDTO.Translate(item));
			}
			return lstDto;
		}
		public NationalPromotionDTO NationalPromotion_SelectByID(int id)
		{
			var rpt = new NationalPromotionRepository();
			return NationalPromotionDTO.Translate(rpt.SelectByID(id));
		}
		public bool NationalPromotion_Insert(NationalPromotionDTO obj)
		{
			var rpt = new NationalPromotionRepository();
			var entity = NationalPromotionDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool NationalPromotion_Delete(int id)
		{
			var rpt = new NationalPromotionRepository();
			return rpt.Delete(id);
		}
		public bool NationalPromotion_Update(NationalPromotionDTO obj)
		{
			var rpt = new NationalPromotionRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<NationalPromotionDTO> NationalPromotion_RetrieveByID(int id)
		{
			var rpt = new NationalPromotionRepository();
			var lstDto = new List<NationalPromotionDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(NationalPromotionDTO.Translate(item));
			}
			return lstDto;
		}
        public NationalPromotionDTO NationalPromotion_SelectByIDItem(int iditem)
		{
			var rpt = new NationalPromotionRepository();
			return NationalPromotionDTO.Translate(rpt.SelectByIDItem(iditem));
		}

        #endregion

        #region LOCALPROMOTION
        
		public List<LocalPromotionDTO> LocalPromotion_SelectAll()
		{
			var lstDto = new List<LocalPromotionDTO>();
			var rpt = new LocalPromotionRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(LocalPromotionDTO.Translate(item));
			}
			return lstDto;
		}
		public LocalPromotionDTO LocalPromotion_SelectByID(int id)
		{
			var rpt = new LocalPromotionRepository();
			return LocalPromotionDTO.Translate(rpt.SelectByID(id));
		}
		public bool LocalPromotion_Insert(LocalPromotionDTO obj)
		{
			var rpt = new LocalPromotionRepository();
			var entity = LocalPromotionDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool LocalPromotion_Delete(int id)
		{
			var rpt = new LocalPromotionRepository();
			return rpt.Delete(id);
		}
        public bool LocalPromotion_DeleteByIDItemAndIDChemistWareHouse(int iditem, int idChemistWareHouse)
        {
            var rpt = new LocalPromotionRepository();
            return rpt.DeleteByIDItemAndIDChemistWareHouse(iditem, idChemistWareHouse);
        }
		public bool LocalPromotion_Update(LocalPromotionDTO obj)
		{
			var rpt = new LocalPromotionRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<LocalPromotionDTO> LocalPromotion_RetrieveByID(int id)
		{
			var rpt = new LocalPromotionRepository();
			var lstDto = new List<LocalPromotionDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(LocalPromotionDTO.Translate(item));
			}
			return lstDto;
		}
		public List<LocalPromotionDTO> LocalPromotion_SelectByIDItem(int iditem)
		{
			var rpt = new LocalPromotionRepository();
			var lstDto = new List<LocalPromotionDTO>();
			var lstEntity = rpt.SelectByIDItem(iditem);
			foreach (var item in lstEntity)
			{
				lstDto.Add(LocalPromotionDTO.Translate(item));
			}
			return lstDto;
		}
		public List<LocalPromotionDTO> LocalPromotion_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			var rpt = new LocalPromotionRepository();
			var lstDto = new List<LocalPromotionDTO>();
			var lstEntity = rpt.SelectByIDChemistWareHouse(idChemistWareHouse);
			foreach (var item in lstEntity)
			{
				lstDto.Add(LocalPromotionDTO.Translate(item));
			}
			return lstDto;
		}
        public LocalPromotionDTO LocalPromotion_SelectByIDItemAndIDChemistWareHouse(int iditem, int idChemistWareHouse)
        {
            var rpt = new LocalPromotionRepository();
            return LocalPromotionDTO.Translate(rpt.SelectByIDItemAndIDChemistWareHouse(iditem, idChemistWareHouse));
        }

        #endregion

        #region ORDERS
        
		public List<OrdersDTO> Orders_SelectAll()
		{
			var lstDto = new List<OrdersDTO>();
			var rpt = new OrdersRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrdersDTO.Translate(item));
			}
			return lstDto;
		}
		public OrdersDTO Orders_SelectByID(int id)
		{
			var rpt = new OrdersRepository();
			return OrdersDTO.Translate(rpt.SelectByID(id));
		}
		public bool Orders_Insert(ref OrdersDTO obj)
		{
			var rpt = new OrdersRepository();
			var entity = OrdersDTO.Reverse(obj);
			var result = rpt.Insert(entity);
            if (result)
            {
                obj.ID = entity.ID;
            }
            return result;
		}
		public bool Orders_Delete(int id)
		{
			var rpt = new OrdersRepository();
			return rpt.Delete(id);
		}
		public bool Orders_Update(OrdersDTO obj)
		{
			var rpt = new OrdersRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<OrdersDTO> Orders_RetrieveByID(int id)
		{
			var rpt = new OrdersRepository();
			var lstDto = new List<OrdersDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrdersDTO.Translate(item));
			}
			return lstDto;
		}
		public List<OrdersDTO> Orders_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
            try
            {
                var rpt = new OrdersRepository();
                var lstDto = new List<OrdersDTO>();
                var lstEntity = rpt.SelectByIDChemistWareHouse(idChemistWareHouse);
                foreach (var item in lstEntity)
                {
                    lstDto.Add(OrdersDTO.Translate(item));
                }
                return lstDto;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return new List<OrdersDTO>();
		}
        public List<OrdersDTO> Orders_SelectByIDChemistWareHouseAndOrderDate(int idChemistWareHouse, DateTime orderdate)
        {
            try
            {
                var rpt = new OrdersRepository();
                var lstDto = new List<OrdersDTO>();
                var lstEntity = rpt.SelectByIDChemistWareHouseAndOrderDate(idChemistWareHouse, orderdate);
                foreach (var item in lstEntity)
                {
                    lstDto.Add(OrdersDTO.Translate(item));
                }
                return lstDto;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return new List<OrdersDTO>();
        }

        public List<OrdersDTO> Orders_SelectByIDCustomer(int idcustomer)
        {
            var rpt = new OrdersRepository();
            var lstDto = new List<OrdersDTO>();
            var lstEntity = rpt.SelectByIDCustomer(idcustomer);
            foreach (var item in lstEntity)
            {
                lstDto.Add(OrdersDTO.Translate(item));
            }
            return lstDto;
        }

        #endregion

        #region ORDERDETAIL
        
		public List<OrderDetailDTO> OrderDetail_SelectAll()
		{
			var lstDto = new List<OrderDetailDTO>();
			var rpt = new OrderDetailRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrderDetailDTO.Translate(item));
			}
			return lstDto;
		}
		public OrderDetailDTO OrderDetail_SelectByID(int id)
		{
			var rpt = new OrderDetailRepository();
			return OrderDetailDTO.Translate(rpt.SelectByID(id));
		}
		public bool OrderDetail_Insert(OrderDetailDTO obj)
		{
			var rpt = new OrderDetailRepository();
			var entity = OrderDetailDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool OrderDetail_Delete(int id)
		{
			var rpt = new OrderDetailRepository();
			return rpt.Delete(id);
		}
        public bool OrderDetail_DeleteByOrder(int idorder)
        {
            var rpt = new OrderDetailRepository();
            return rpt.DeleteByOrder(idorder);
        }
		public bool OrderDetail_Update(OrderDetailDTO obj)
		{
			var rpt = new OrderDetailRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<OrderDetailDTO> OrderDetail_RetrieveByID(int id)
		{
			var rpt = new OrderDetailRepository();
			var lstDto = new List<OrderDetailDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrderDetailDTO.Translate(item));
			}
			return lstDto;
		}
		public List<OrderDetailDTO> OrderDetail_SelectByIDOrder(int idorder)
		{
			var rpt = new OrderDetailRepository();
			var lstDto = new List<OrderDetailDTO>();
			var lstEntity = rpt.SelectByIDOrder(idorder);
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrderDetailDTO.Translate(item));
			}
			return lstDto;
		}
		public List<OrderDetailDTO> OrderDetail_SelectByIDItem(int iditem)
		{
			var rpt = new OrderDetailRepository();
			var lstDto = new List<OrderDetailDTO>();
			var lstEntity = rpt.SelectByIDItem(iditem);
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrderDetailDTO.Translate(item));
			}
			return lstDto;
		}

        #endregion

        #region ROLEFUNCTIONS
        
		public List<RoleFunctionsDTO> RoleFunctions_SelectAll()
		{
			var lstDto = new List<RoleFunctionsDTO>();
			var rpt = new RoleFunctionsRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(RoleFunctionsDTO.Translate(item));
			}
			return lstDto;
		}
		public RoleFunctionsDTO RoleFunctions_SelectByID(int id)
		{
			var rpt = new RoleFunctionsRepository();
			return RoleFunctionsDTO.Translate(rpt.SelectByID(id));
		}
		public bool RoleFunctions_Insert(RoleFunctionsDTO obj)
		{
			var rpt = new RoleFunctionsRepository();
			var entity = RoleFunctionsDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool RoleFunctions_Delete(int id)
		{
			var rpt = new RoleFunctionsRepository();
			return rpt.Delete(id);
		}
		public bool RoleFunctions_Update(RoleFunctionsDTO obj)
		{
			var rpt = new RoleFunctionsRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<RoleFunctionsDTO> RoleFunctions_RetrieveByID(int id)
		{
			var rpt = new RoleFunctionsRepository();
			var lstDto = new List<RoleFunctionsDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(RoleFunctionsDTO.Translate(item));
			}
			return lstDto;
		}
		public List<RoleFunctionsDTO> RoleFunctions_SelectByIDRole(int idrole)
		{
			var rpt = new RoleFunctionsRepository();
			var lstDto = new List<RoleFunctionsDTO>();
			var lstEntity = rpt.SelectByIDRole(idrole);
			foreach (var item in lstEntity)
			{
				lstDto.Add(RoleFunctionsDTO.Translate(item));
			}
			return lstDto;
		}
		public List<RoleFunctionsDTO> RoleFunctions_SelectByIDFunction(int idfunction)
		{
			var rpt = new RoleFunctionsRepository();
			var lstDto = new List<RoleFunctionsDTO>();
			var lstEntity = rpt.SelectByIDFunction(idfunction);
			foreach (var item in lstEntity)
			{
				lstDto.Add(RoleFunctionsDTO.Translate(item));
			}
			return lstDto;
		}

        #endregion

        #region FUNCTIONS
        
		public List<FunctionsDTO> Functions_SelectAll()
		{
			var lstDto = new List<FunctionsDTO>();
			var rpt = new FunctionsRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(FunctionsDTO.Translate(item));
			}
			return lstDto;
		}
		public FunctionsDTO Functions_SelectByID(int id)
		{
			var rpt = new FunctionsRepository();
			return FunctionsDTO.Translate(rpt.SelectByID(id));
		}
		public bool Functions_Insert(FunctionsDTO obj)
		{
			var rpt = new FunctionsRepository();
			var entity = FunctionsDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Functions_Delete(int id)
		{
			var rpt = new FunctionsRepository();
			return rpt.Delete(id);
		}
		public bool Functions_Update(FunctionsDTO obj)
		{
			var rpt = new FunctionsRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<FunctionsDTO> Functions_RetrieveByID(int id)
		{
			var rpt = new FunctionsRepository();
			var lstDto = new List<FunctionsDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(FunctionsDTO.Translate(item));
			}
			return lstDto;
		}

        #endregion

        #region ROLE
        
		public List<RoleDTO> Role_SelectAll()
		{
			var lstDto = new List<RoleDTO>();
			var rpt = new RoleRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(RoleDTO.Translate(item));
			}
			return lstDto;
		}
		public RoleDTO Role_SelectByID(int id)
		{
			var rpt = new RoleRepository();
			return RoleDTO.Translate(rpt.SelectByID(id));
		}
		public bool Role_Insert(RoleDTO obj)
		{
			var rpt = new RoleRepository();
			var entity = RoleDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Role_Delete(int id)
		{
			var rpt = new RoleRepository();
			return rpt.Delete(id);
		}
		public bool Role_Update(RoleDTO obj)
		{
			var rpt = new RoleRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<RoleDTO> Role_RetrieveByID(int id)
		{
			var rpt = new RoleRepository();
			var lstDto = new List<RoleDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(RoleDTO.Translate(item));
			}
			return lstDto;
		}

        #endregion

        #region CHEMIST WAREHOUSE

        public List<ChemistWareHouseDTO> ChemistWareHouse_SelectAll()
		{
			var lstDto = new List<ChemistWareHouseDTO>();
			var rpt = new ChemistWareHouseRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(ChemistWareHouseDTO.Translate(item));
			}
			return lstDto;
		}
		public ChemistWareHouseDTO ChemistWareHouse_SelectByID(int id)
		{
			var rpt = new ChemistWareHouseRepository();
			return ChemistWareHouseDTO.Translate(rpt.SelectByID(id));
		}
		public bool ChemistWareHouse_Insert(ChemistWareHouseDTO obj)
		{
			var rpt = new ChemistWareHouseRepository();
			var entity = ChemistWareHouseDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool ChemistWareHouse_Delete(int id)
		{
			var rpt = new ChemistWareHouseRepository();
			return rpt.Delete(id);
		}
		public bool ChemistWareHouse_Update(ChemistWareHouseDTO obj)
		{
			var rpt = new ChemistWareHouseRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<ChemistWareHouseDTO> ChemistWareHouse_RetrieveByID(int id)
		{
			var rpt = new ChemistWareHouseRepository();
			var lstDto = new List<ChemistWareHouseDTO>();
			var lstEntity = rpt.RetrieveByID(id);
			foreach (var item in lstEntity)
			{
				lstDto.Add(ChemistWareHouseDTO.Translate(item));
			}
			return lstDto;
        }

        #endregion

        #region CUSTOMER

        public List<CustomerDTO> Customer_SelectAll()
        {
            var lstDto = new List<CustomerDTO>();
            var rpt = new CustomerRepository();
            var lstEntity = rpt.SelectAll();
            foreach (var item in lstEntity)
            {
                lstDto.Add(CustomerDTO.Translate(item));
            }
            return lstDto;
        }
        public CustomerDTO Customer_SelectByID(int id)
        {
            var rpt = new CustomerRepository();
            return CustomerDTO.Translate(rpt.SelectByID(id));
        }
        public bool Customer_Insert(CustomerDTO obj)
        {
            var rpt = new CustomerRepository();
            var entity = CustomerDTO.Reverse(obj);
            return rpt.Insert(entity);
        }
        public bool Customer_Delete(int id)
        {
            var rpt = new CustomerRepository();
            return rpt.Delete(id);
        }
        public bool Customer_Update(CustomerDTO obj)
        {
            var rpt = new CustomerRepository();
            var entity = rpt.SelectByID(obj.ID);
            obj.UpdateEntity(ref entity);
            return rpt.Save();
        }
        public List<CustomerDTO> Customer_RetrieveByID(int id)
        {
            var rpt = new CustomerRepository();
            var lstDto = new List<CustomerDTO>();
            var lstEntity = rpt.RetrieveByID(id);
            foreach (var item in lstEntity)
            {
                lstDto.Add(CustomerDTO.Translate(item));
            }
            return lstDto;
        }

        public CustomerDTO Customer_SelectByUserName(string username)
        {
            var rpt = new CustomerRepository();
            return CustomerDTO.Translate(rpt.SelectByUserName(username));
        }

        public CustomerDTO Customer_SelectByUserNameAndPassword(string username, string password)
        {
            var rpt = new CustomerRepository();
            return CustomerDTO.Translate(rpt.SelectByUserNameAndPassword(username, password));
        }

        #endregion

        #region CART

        public List<CartDTO> Cart_SelectByCartID(string cartid)
        {
            var rpt = new CartRepository();
            var lstDto = new List<CartDTO>();
            var lstEntity = rpt.SelectByCartID(cartid);
            foreach (var item in lstEntity)
            {
                lstDto.Add(CartDTO.Translate(item));
            }
            return lstDto;
        }
        public CartDTO Cart_SelectByIDAndRecordID(string cartid, string recordid)
        {
            var rpt = new CartRepository();
            return CartDTO.Translate(rpt.SelectByIDAndRecordID(cartid, int.Parse(recordid)));
        }
        public CartDTO Cart_SelectByRecordID(string recordid)
        {
            var rpt = new CartRepository();
            return CartDTO.Translate(rpt.SelectByRecordID(int.Parse(recordid)));
        }
        public CartDTO Cart_SelectByIDAndItem(string cartid, string foodid)
        {
            var rpt = new CartRepository();
            return CartDTO.Translate(rpt.SelectByIDAndItem(cartid, int.Parse(foodid)));
        }
        public bool Cart_Insert(CartDTO obj)
        {
            var rpt = new CartRepository();
            var entity = CartDTO.Reverse(obj);
            return rpt.Insert(entity);
        }
        public bool Cart_Delete(string cartid, string recordid)
        {
            var rpt = new CartRepository();
            return rpt.Delete(cartid, int.Parse(recordid));
        }
        public bool Cart_Empty(string cartid)
        {
            var rpt = new CartRepository();
            return rpt.Empty(cartid);
        }
        public bool Cart_Update(CartDTO obj)
        {
            var rpt = new CartRepository();
            var entity = rpt.SelectByIDAndItem(obj.CartId, obj.ItemID);
            obj.UpdateEntity(ref entity);
            return rpt.Save();
        }

        #endregion
    }
}
